package com.geek.javase.ch17.work.tcpwork.tcp2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/*
 *@Date 2025/11/4
 *@Author wangpeng
 *@Description
 * 需求2：服务端接收多条消息，客户端发送多条消息
 */
public class TCPClient {
    public static void main(String[] args) {
        System.out.println("-------客户端--------");
        Socket socket = null;
        PrintWriter pw = null;
        try{
            //创建客户端,连接服务器
            socket = new Socket(InetAddress.getLocalHost(), 8899);
            //写消息
            pw = new PrintWriter(socket.getOutputStream());
            Scanner sc = new Scanner(System.in);
            //循环写消息
            while(true){
                System.out.println("客户端写:");
                String msg = sc.nextLine();
                pw.println(msg);
                pw.flush();
                if(msg.equals("exit")){
                    break;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
